<?php
/**
 * Bootstrap class for Werfpop.
 * 
 * @author hcderaad
 * @package Werfpop
 */

/**
 * Bootstrap class for Werfpop.
 *
 * @author hcderaad
 * @package Werfpop
 */
class Bootstrap {
	/**
	 * The frontcontroller
	 *
	 * @var Zend_Controller_Front
	 */
	public static $frontController = null;
		
	/**
	* The current base directory.
	*
	* @var string
	*/
	public static $rootDir;
	
	/**
	* The register.
	* 
	* @var Zend_Registry
	*/
	public static $registry;
		
	/**
	 * Executes the preparation methods and returns a response.
	 *
	 * @return void
	 */
	public static function run() {
		try {
			self::prepare();
		}
		catch (Exception $e) {
			echo 'Uncaught exception: ' . PHP_EOL;
			echo $e->getMessage() . PHP_EOL;
			echo $e->getTraceAsString();
		}
		$response = self::$frontController->dispatch();
		self::sendResponse($response);
	}
	
	/**
	 * Executes the setupFrontController and setupView methods
	 * 
	 * @return void
	 */
	public static function prepare() {
		self::setupRegistry();
		self::setupConfiguration();
		self::setupCache();
		self::setupDatabase();
		self::setupEnvironment();
		self::setupFrontController();
		self::setupView();
		self::setupTranslation();
		self::setupAcl();
	}
	
	/**
	 * Instantiates the Zend_Config_Ini object.
	 *
	 * @return void
	 */
	public static function setupConfiguration() {
		self::$registry->configFile = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.ini';
		self::$registry->config = new Zend_Config_Ini(self::$registry->configFile);
	}
	
	/**
	 * Setup the caching
	 *
	 * @return void
	 */
	public static function setupCache() {
		$dir = realpath(self::$registry->config->bootstrap->base_dir . self::$registry->config->cache->backend->dir)
				. DIRECTORY_SEPARATOR;
		$frontendOpts = array("automatic_serialization" => true,
								"cache_id_prefix" => self::$registry->config->cache->frontend->id_prefix,
								"automatic_cleaning_factor" => 1,
								"ignore_user_abort" => 1,
								"write_control" => self::$registry->config->cache->frontend->write_control,
								"lifetime" => self::$registry->config->cache->frontend->lifetime);
		$backendOpts = array("cache_dir" => $dir);
		
		self::$registry->cache = Zend_Cache::factory("Core","File",$frontendOpts,$backendOpts);
	}
	
	/**
	 * Sets the environment settings
	 * 
	 * @return void
	 */
	public static function setupEnvironment() {
		error_reporting(E_ALL|E_STRICT);
		ini_set('display_errors',self::$registry->config->bootstrap->display_errors);
		date_default_timezone_set(self::$registry->config->bootstrap->default_timezone);
		self::$rootDir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
		self::$registry->server = (object) array();
		self::$registry->server->server_name = 'http://' . $_SERVER['SERVER_NAME'];
		self::$registry->server->request_uri = $_SERVER['SCRIPT_NAME'];
		self::$registry->server->base_url = self::$registry->server->server_name . self::$registry->server->request_uri;
	}
	
	/**
	* Initialises the registry.
	* @return void
	*/
	public static function setupRegistry() {
		self::$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
		Zend_Registry::setInstance(self::$registry);
	}
	
	/**
	 * Gets an instance of Zend_Controller_Front and sets it to the local frontController var.
	 *
	 * @return void
	 */
	public static function setupFrontController() {
		self::$frontController = Zend_Controller_Front::getInstance();
		self::$frontController->throwExceptions(true);
		self::$frontController->returnResponse(true);
		self::$frontController->setControllerDirectory(array(
			'default' => self::$rootDir . 'application' . DIRECTORY_SEPARATOR . 'controllers',
			'admin' => self::$rootDir . 'application' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'controllers'
			)
		);
		self::$frontController->registerPlugin(new Zend_Controller_Plugin_ErrorHandler());
		if(!empty(self::$registry->config->bootstrap->base_url)) {
			self::$frontController->setBaseUrl(self::$registry->config->bootstrap->base_url);
		}
		self::$frontController->setParam('registry',self::$registry);
	}
	
	/**
	 * Setup of the Zend_View object.
	 *
	 * @return void
	 */
	public static function setupView() {
		$view = new Zend_View;
		$view->setEncoding(self::$registry->config->bootstrap->text_encoding);
		$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
		Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
		Zend_Layout::startMvc(array('layoutPath' => self::$rootDir . DIRECTORY_SEPARATOR . 'application' 
										. DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts',
									'layout' => 'common',
									'pluginClass' => 'EICms_Controller_Plugin_Layout'));	
	}
	
	/**
	* Initialises the database.
	* @return void
	*/
	public static function setupDatabase() {
		self::$registry->db = Zend_Db::factory(self::$registry->config->database->adapter,self::$registry->config->database->param->toArray());
		self::$registry->db->query('SET NAMES \'' . self::$registry->config->database->encoding . '\'');
		Zend_Db_Table::setDefaultAdapter(self::$registry->db);
		Zend_Db_Table_Abstract::setDefaultMetadataCache(self::$registry->cache);
	}
	
	/**
	 * Initializes the translation
	 *
	 */
	public static function setupTranslation() {
		Zend_Translate::setCache(self::$registry->get('cache'));
		$defFile = realpath('..' . DIRECTORY_SEPARATOR . 'language' . DIRECTORY_SEPARATOR . 'en.csv');
		if(!file_exists($defFile)) {
			throw new EICms_Exception('Default language file missing: ' . $defFile);
		}
		$fileType = substr($defFile,strpos($defFile,'.') + 1);
		$trans = new Zend_Translate($fileType,$defFile,'en',array('delimiter' => '|'));
		self::$registry->set('translate',$trans);
		if(self::$registry->get('config')->meta->language != 'en') {
			$langFile = realpath('..' . DIRECTORY_SEPARATOR . 'language' . DIRECTORY_SEPARATOR . self::$registry->get('config')->meta->language . '.csv');
			if(!file_exists($langFile)) {
				throw new EICms_Exception('Configured language file missing: ' . $langFile);
			}
			$trans->addTranslation($langFile,self::$registry->get('config')->meta->language);
		}
	}
	
	/**
	 * Initialises the access controll list.
	 *
	 */
	public static function setupAcl() {
		$auth = EICms_Auth::getInstance();
		$acl = new EICms_Acl($auth);
		self::$frontController->setParam('auth',$auth);
		self::$frontController->setParam('acl',$acl);
		self::$frontController->registerPlugin(new EICms_Controller_Plugin_Acl($auth,$acl));
	}
	
	/**
	 * Sends the response
	 *
	 * @param Zend_Controller_Response_Http $response
	 */
	public static function sendResponse(Zend_Controller_Response_Http $response) {
		$response->setHeader('Content-Type','text/html; charset=' . self::$registry->config->bootstrap->text_encoding,true);
		$response->sendResponse();
	}
}

?>